package com.iule.common.playboy.utils;

import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * The tool of handing Json type in mysql
 *  Mysql处理json的方法 * 1.0.0仅支持全查询 *
 * @author 柚子
 * @date {2020/3/16}
 */
public class JSONSql {

    public static final char UNDERLINE = '_';

    private Class aClass;

    private String tableName;

    private StringBuilder sqlBuilder;

    public JSONSql(){
        tableName = "control_config";
        sqlBuilder = new StringBuilder("select * from control_config where 1=1 ");
    }

    public JSONSql(Class clazz){
        aClass = clazz;
        tableName = camelToUnderline(clazz.getSimpleName().toLowerCase(),1);
        sqlBuilder = new StringBuilder(String.format("select * from  %s  where 1=1", tableName)).append(" ");
    }


    /////////////////////////////////////////////////


    public  JSONSql eq(String fieldName,Object value) {
        start();
        condition(fieldName, "=", value);
        end();
        return this;
    }

    public  JSONSql lt(String fieldName,Object value) {
        start();
        condition(fieldName, "<", value);
        end();
        return this;
    }

    public  JSONSql le(String fieldName,Object value) {
        start();
        condition(fieldName, "<=", value);
        end();
        return this;
    }

    public  JSONSql gt(String fieldName,Object value) {
        start();
        condition(fieldName, ">", value);
        end();
        return this;
    }

    public  JSONSql ge(String fieldName,Object value) {
        start();
        condition(fieldName, ">=", value);
        end();
        return this;
    }

    private void start() {
        sqlBuilder.append("and").append(" ");
    }

    private void condition(String fieldName,String condition,Object value) {
        if (value instanceof Number) {
            sqlBuilder.append(String.format("%s->'$.%s' %s %s", tableName, fieldName,condition, value));
        } else {
            sqlBuilder.append(String.format("%s->'$.%s' %s '%s'", tableName, fieldName,condition, value));
        }
    }

    private void end() {
        sqlBuilder.append(" ");
    }

    // 驼峰转下划线
    public static String camelToUnderline(String param, Integer charType) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (Character.isUpperCase(c)) {
                sb.append(UNDERLINE);
            }
            if (charType == 2) {
                sb.append(Character.toUpperCase(c));  //统一都转大写
            } else {
                sb.append(Character.toLowerCase(c));  //统一都转小写
            }
        }
        return sb.toString();
    }

    //下划线转驼峰
    public static String underlineToCamel(String param) {
        if (param == null || "".equals(param.trim())) {
            return "";
        }
        int len = param.length();
        StringBuilder sb = new StringBuilder(len);
        Boolean flag = false; // "_" 后转大写标志,默认字符前面没有"_"
        for (int i = 0; i < len; i++) {
            char c = param.charAt(i);
            if (c == UNDERLINE) {
                flag = true;
                continue;   //标志设置为true,跳过
            } else {
                if (flag == true) {
                    //表示当前字符前面是"_" ,当前字符转大写
                    sb.append(Character.toUpperCase(param.charAt(i)));
                    flag = false;  //重置标识
                } else {
                    sb.append(Character.toLowerCase(param.charAt(i)));
                }
            }
        }
        return sb.toString();
    }

    @Override
    public String toString() {
        return sqlBuilder.toString();
    }


}
